#include "bigimg.h"
#include <nds.h>
  
#include <filesystem.h>
  
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>


void setupOrtho()
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	glOrtho(0, 256, 192, 0, 0.1, 100);
	gluLookAt(	0.0, 0.0, 0.1,		//camera possition 
				0.0, 0.0, 0.0,		//look at
				0.0, 1.0, 0.0);		//up
}

void setupPerspective()
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(70, 256.0 / 192.0, 0.1, 40);
	gluLookAt(	0.0, 0.0, 1.0,		//camera possition 
				0.0, 0.0, 0.0,		//look at
				0.0, 1.0, 0.0);		//up
}

void initVideo()
{
	//set mode 0, enable BG0 and set it to 3D
	videoSetMode(MODE_0_3D);

	vramSetBankA(VRAM_A_TEXTURE);
	vramSetBankB(VRAM_B_TEXTURE);
	vramSetBankC(VRAM_C_TEXTURE);
	vramSetBankD(VRAM_D_TEXTURE);
}

u8 bmpimg[256*192];
int bg3sub;
char sign=0;
void drawImage2D()
{
    int i;
    uint8_t c;

    if(sign) for(i=0,c=0; i<256*192;i++) bmpimg[i]=c++;
    else for(i=0,c=255; i<256*192;i++) bmpimg[i]=c--;

    sign ^= 1;
}

void initSubVideoBMP()
{
    videoSetModeSub(MODE_5_2D);
    vramSetBankH(VRAM_H_SUB_BG);
    vramSetBankI(VRAM_I_SUB_BG_0x06208000);

    int r,g,b,c=0;
    for(r=0;r<=7;r++) for(g=0;g<=7;g++) for(b=0;b<=3;b++) BG_PALETTE_SUB[c++] = RGB15( (31*r)/7, (31*g)/7, (31*b)/3);

    bg3sub = bgInitSub(3, BgType_Bmp8, BgSize_B8_256x256, 0,0);
}

void initGL()
{
	// initialize gl
	glInit();

	// enable antialiasing
	glEnable(GL_ANTIALIAS);

	// setup the rear plane
	glClearColor(0,0,0,31); // BG must be opaque for AA to work
	glClearPolyID(63); // BG must have a unique polygon ID for AA to work
	glClearDepth(0x7FFF);

	//this should work the same as the normal gl call
	glViewport(0,0,255,191);

	setupOrtho();
//	setupPerspective();

	glMatrixMode(GL_TEXTURE);
	glLoadIdentity();

//	glColor3f(1,1,1);
//	glLight(0, RGB15(31,31,31) , 0, 0,floattov10(-1.0));

	glEnable(GL_TEXTURE_2D);
//	glMaterialf(GL_AMBIENT, RGB15(16,16,16));
//	glMaterialf(GL_DIFFUSE, RGB15(16,16,16));
//	glMaterialf(GL_SPECULAR, BIT(15) | RGB15(8,8,8)); 
//	glMaterialf(GL_EMISSION, RGB15(16,16,16)); //emission required to view textures
	glMaterialf(GL_EMISSION, RGB15(31,31,31)); //emission required to view textures

	//ds uses a table for shinyness..this generates a half-ass one
//	glMaterialShinyness();

//	glEnable(GL_BLEND);

//	glEnable(GL_ALPHA_TEST);
//	glAlphaFunc(1);
}

int main()
{	
	int camy=0, camx=0, i;
	BigImage *img;
	BigImage *img2;
	BigImage *img3;

	nitroFSInit(NULL);

	initVideo();
        initSubVideoBMP();

	initGL();

	img = createBigImage("/texture.bin", 60, 37);
	loadTextures(img);

//	img2 = createBigImage("/texture.bin", 60, 37);
//	loadTextures(img2);

//	img3 = createBigImage("/texture.bin", 60, 37);
//	loadTextures(img3);

	while(1)
	{
		glMatrixMode(GL_MODELVIEW);
		glPushMatrix();

		drawImg(img);
//		drawImg(img2);
//		drawImg(img3);

		glPopMatrix(1);

		glFlush(0);
		scanKeys();
		
		u16 keys = keysHeld();

		if((keys & KEY_UP)) {camy-=1;}
		if((keys & KEY_DOWN)) {camy+=1;}
		if((keys & KEY_LEFT)) {camx-=1;}
		if((keys & KEY_RIGHT)) {camx+=1;}		

drawImage2D();

scrollTo(img,camx,camy);
//scrollTo(img2,camx,camy);
//scrollTo(img3,camx,camy);
		swiWaitForVBlank();
loadTextures(img);
//loadTextures(img2);
//loadTextures(img3);

dmaCopy(bmpimg, bgGetGfxPtr(bg3sub), 256*192);
	}

	return 0;
}//end main 
